package com.ff.neuropp.service;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.ff.neuropp.domain.User;
import com.ff.neuropp.repository.UserRepository;
import com.ff.neuropp.util.SpecificationUtil;

@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    /**
     * 创建实体
     *
     * @param user
     */
    public User saveUser(User user) {
        return userRepository.save(user);
    }

    /**
     * 查找实体
     *
     * @param id
     * @return
     */
    public User findById(Integer id) {
        Optional<User> optional = userRepository.findById(id);
        if (optional.isPresent()) {
            return optional.get();
        } else {
            return null;
        }
    }

    public void deleteUser(Integer id) {
        userRepository.deleteById(id);
    }

    /**
     * 分页条件查询
     *
     * @param page
     * @param size
     * @param param
     * @return
     */
    public Page<User> findPage(int page, int size, Map<String, Object> param) {
        Sort.Direction direction = Sort.Direction.DESC;
        String sort = "id";
        Pageable pageable = new PageRequest(page - 1, size, direction, sort);
        return userRepository.findAll(SpecificationUtil.buildSpecification(param, User.class), pageable);
    }

    /**
     * 根据条件查询List
     */
    public List<User> findList(Map<String, Object> param) {
        return userRepository.findAll(SpecificationUtil.buildSpecification(param, User.class));
    }

    public Page<User> findPage(int page, int size, String carShopId, String tag) {
        Sort.Direction direction = Sort.Direction.DESC;
        String sort = "id";
        Pageable pageable = new PageRequest(page-1, size, direction, sort);
        return userRepository.findAll(new Specification<User>() {
            @Override
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Predicate p = criteriaBuilder.and(criteriaBuilder.greaterThan(root.get("id").as(Integer.class), 0));

                if (null != carShopId && carShopId.length() > 0) {
                    p = criteriaBuilder.and(criteriaBuilder.equal(root.get("car4sShop").get("id").as(String.class), carShopId), p);
                }
                if (null != tag && tag.length() > 0) {
                    Predicate p1 = criteriaBuilder.like(root.get("carNum").as(String.class), "%" + tag + "%");
                    Predicate p2 = criteriaBuilder.like(root.get("carFrame").as(String.class), "%" + tag + "%");
                    Predicate orp = criteriaBuilder.or(p1);
                    orp = criteriaBuilder.or(p2, orp);
                    p = criteriaBuilder.and(p, orp);
                }
                return p;
            }
        }, pageable);
    }

    public User findUserByAccount(String account) {
        Optional<User> optional = userRepository.findOne(new Specification<User>() {
            @Override
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Predicate p = criteriaBuilder.and(criteriaBuilder.equal(root.get("account").as(String.class), account));
                return p;
            }
        });
        if (optional.isPresent()) {
            return optional.get();
        } else {
            return null;
        }
    }


    public List<User> findListByTag( String tag) {

        return userRepository.findAll(new Specification<User>() {
            @Override
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Predicate p = criteriaBuilder.and(criteriaBuilder.greaterThan(root.get("id").as(Integer.class), 0));

                if (null != tag && tag.length() > 0) {
                    Predicate p1 = criteriaBuilder.like(root.get("name").as(String.class), "%" + tag + "%");
                    Predicate p2 = criteriaBuilder.like(root.get("account").as(String.class), "%" + tag + "%");
                    Predicate p3 = criteriaBuilder.like(root.get("phone").as(String.class), "%" + tag + "%");
                    Predicate orp = criteriaBuilder.or(p1);
                    orp = criteriaBuilder.or(p2, orp);
                    orp = criteriaBuilder.or(p3, orp);
                    p = criteriaBuilder.and(p, orp);
                }
                return p;
            }
        });
    }
}
